package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.StrategyComment;
import cn.wolfcode.luowowo.comment.query.StrategyCommentQuery;
import cn.wolfcode.luowowo.comment.repository.IStrategyCommentRepository;
import cn.wolfcode.luowowo.comment.service.IStrategyCommentService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by LENOVO on 2019/8/13.
 */

/**
 *
 *
 *
 *
 */
@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService{


    @Autowired
    private IStrategyCommentRepository repository;
    @Autowired
    private MongoTemplate template;


    @Override
    public void saveOrUpdate(StrategyComment comment) {
        //判断文档的主键id是否空，如果为空就设置创建时间，否则不设置
            if (!StringUtils.hasLength(comment.getId())){
                    //创建
                comment.setCreateTime(new Date());
            }
            repository.save(comment);
    }

    @Override
    public Page query(StrategyCommentQuery qo) {
        //查询条件
        Query query =new Query();
        if(qo.getDetailId() !=-1){
            //攻略id 作为条件
            query.addCriteria(Criteria.where("detailId").is(qo.getDetailId()));
        }
        //查询总数并进行判断
        Long count=template.count(query,StrategyComment.class);
        if(count==0){
            return Page.empty();
        }
        //查询某页的 List
        //select * from xxx where .... order by
        //参数1:当前页, 注意, 从0开始
        //参数2:每页显示条数
        //参数3:排序规则, 参数a: 表示倒序  参数b:哪个字段
        PageRequest pageable=PageRequest.of(qo.getCurrentPage()-1,qo.getPageSize()
                                , Sort.by(Sort.Direction.DESC,"createTime"));
        query.with(pageable);

        List<StrategyComment> list=template.find(query,StrategyComment.class);//这个list是总数据
        return new PageImpl(list,pageable,count);


    }

    @Override
    public void commentThumbUp(String toid, Long userId) {
        //获取当前操作的评论的对象
        Optional<StrategyComment> op = repository.findById(toid);

        StrategyComment comment=op.get();
        if(comment !=null){
            //2.获取评论中心点赞用户的id列表
            List<Long> list = comment.getThumbuplist();
           //3.判断传入 userId是否存在用户 id 列表
            if(list.contains(userId)){
                //如果存在就取消点赞
                comment.setThumbupnum(comment.getThumbupnum()-1);
                list.remove(userId);
            }else{
                //如果不存在，就是点赞操作
                comment.setThumbupnum(comment.getThumbupnum()+1);
                list.add(userId);
            }
        }
        this.saveOrUpdate(comment);
    }
}
